$(function () {
    $("#jqGrid").jqGrid({
        url: baseURL + 'lanling/questionrecord/list',
        datatype: "json",
        colModel: [
            {label: '学员名', name: 'userName', index: 'userName', width: 80, align: "center"},
            {label: '题目', name: 'questionContent', questionContent: 'truNum', width: 180, align: "center"},
            {label: '学员答案', name: 'studentAnswer', index: 'studentAnswer', width: 80, align: "center"},
            {label: '正确答案', name: 'trueAnswer', index: 'trueAnswer', width: 80, align: "center"},
            {label: '记录时间', name: 'createDate', index: 'createDate', width: 80, align: "center"}
        ],
        viewrecords: true,
        height: 385,
        rowNum: 10,
        rowList: [10, 30, 50],
        rownumbers: true,
        rownumWidth: 25,
        autowidth: true,
        multiselect: true,
        pager: "#jqGridPager",
        jsonReader: {
            root: "page.list",
            page: "page.currPage",
            total: "page.totalPage",
            records: "page.totalCount"
        },
        prmNames: {
            page: "page",
            rows: "limit",
            order: "order"
        },
        gridComplete: function () {
            //隐藏grid底部滚动条
            $("#jqGrid").closest(".ui-jqgrid-bdiv").css({"overflow-x": "hidden"});
        }
    });
});

var vm = new Vue({
        el: '#rrapp',
        data: {
            showList: true,
            title: null,
            questionRecord: {},
            ruleValidate: {
                name: [
                    {required: true, message: 'name不能为空', trigger: 'blur'},
                    {max: 100, message: '请输入最多100位'}
                ]
            },
            q: {
                name: '',
                studentId: null
            },
            studentList: []
        },
        mounted() {
            this.getStudentList()
        },
        methods: {
            query: function () {
                vm.reload();
            }
            ,
            add: function () {
                vm.showList = false;
                vm.title = "新增";
                vm.questionRecord = {};
            }
            ,
            update: function (event) {
                var id = getSelectedRow();
                if (id == null) {
                    return;
                }
                vm.showList = false;
                vm.title = "修改";

                vm.getInfo(id)
            }
            ,
            saveOrUpdate: function (event) {
                var url = vm.questionRecord.id == null ? "lanling/questionrecord/save" : "lanling/questionrecord/update";
                $.ajax({
                    type: "POST",
                    url: baseURL + url,
                    contentType: "application/json;charset=utf-8",
                    data: JSON.stringify(vm.questionRecord),
                    success: function (r) {
                        if (r.code === 0) {
                            alert('操作成功', function (index) {
                                vm.reload();
                            });
                        } else {
                            alert(r.msg);
                        }
                    }
                });
            }
            ,
            del: function (event) {
                var ids = getSelectedRows();
                if (ids == null) {
                    return;
                }

                confirm('确定要删除选中的记录？', function () {
                    $.ajax({
                        type: "POST",
                        url: baseURL + "lanling/questionrecord/delete",
                        contentType: "application/json;charset=utf-8",
                        data: JSON.stringify(ids),
                        success: function (r) {
                            if (r.code == 0) {
                                alert('操作成功', function (index) {
                                    $("#jqGrid").trigger("reloadGrid");
                                });
                            } else {
                                alert(r.msg);
                            }
                        }
                    });
                });
            }
            ,
            getInfo: function (id) {
                $.get(baseURL + "lanling/questionrecord/info/" + id, function (r) {
                    vm.questionRecord = r.questionRecord;
                });
            }
            ,
            reload: function (event) {
                vm.showList = true;
                var page = $("#jqGrid").jqGrid('getGridParam', 'page');
                $("#jqGrid").jqGrid('setGridParam', {
                    postData: {'name': vm.q.name, 'studentId': vm.q.studentId},
                    page: page
                }).trigger("reloadGrid");
                vm.handleReset('formValidate');
            }
            ,
            handleSubmit: function (name) {
                handleSubmitValidate(this, name, function () {
                    vm.saveOrUpdate()
                });
            }
            ,
            handleReset: function (name) {
                handleResetForm(this, name);
            }
            ,
            getStudentList: function () {
                $.get("../../lanling/student/queryList", function (r) {
                    vm.studentList = r.list;
                });
            }
        }
    })
;